﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using XoLib.Serialization;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Globalization;

namespace XoLib.Menu
{

#if !(WindowsCE || OldMenu)

    /// <summary>
    /// Store data form the xml file
    /// <example> key = ParseKeys("Control+B"); </example>/// 
    /// </summary>
    public class MenuMetaData 
    {

        internal MenuMetaData( string name, ushort code, string tag)
        {
            this.Name = name;
            this.Code = code;
            this.Tag = tag;
        }

        /// <summary>
        /// Name
        /// <returns>string</returns>
        /// </summary>
        public string Name { get; internal set; }
        /// <summary>
        /// Code
        /// <returns>ushort</returns>
        /// </summary>
        public ushort Code { get; internal set; }
        /// <summary>
        /// Tag
        /// <returns>string</returns>
        /// </summary>
        public string Tag  { get; internal set; }
    }


    /// <summary>
    /// Class de contruction of Menu
    /// </summary>
    public abstract class BuildMainStrip : BuildStrip
    {

        /// <summary>
        /// Parse key string
        /// <para>
        /// <param name="filename">string</param>
        /// </para>
        /// <example> new BuildMainStrip("Menu.xml"); </example>/// 
        /// <returns>Keys</returns>
        /// </summary>
        public BuildMainStrip(string filename):
            base (filename)
        {
        }

        /// <summary>
        /// Build data
        /// <para>
        /// <param name="nodelist">XmlNodeList</param>
        /// </para>
        /// <returns>ToolStripItems</returns>
        /// </summary>
        public override ToolStripItems Parcourir(XmlNodeList nodelist)
        {
            ToolStripMenuItem Maitre = null;
            ToolStripItem Child = null;
           
            string sFile = null;
            ToolStripItems result = new ToolStripItems();

            foreach (XmlNode level in nodelist)
            {
                string name;
                switch(name = level.Name.ToLower())
                {
                    case "menu":
                        if ((sFile = XmlNodeExtention.Get(level,"File")) != null)
                           {
                                var load = Load(sFile);
                                if (load != null)
                                    result.AddRange(load.ToArray());
                                continue;
                            }
                        break;
                    case "split":
                            Child = new ToolStripSeparator();
                            result.Add(Child);
                            continue;
                       // break;
                }

                string sName = XmlNodeExtention.Get(level, "Name");
                ushort uCode = XmlNodeExtention.Get<ushort>(level, "Code");
                string sImage = XmlNodeExtention.Get(level,"Image");
                Maitre = new ToolStripMenuItem(sName, resources != null ? ((System.Drawing.Image)(resources.GetObject(sImage))) : default(Image));
                Keys Valuekey;

                if (ToolStripManager.IsValidShortcut(Valuekey = ParseKeys(XmlNodeExtention.Get(level, "Key"))))
                {
                    Maitre.ShowShortcutKeys = XmlNodeExtention.Get<bool>(level, "Show");
                    Maitre.ShortcutKeys = Valuekey;
                }

                Maitre.Tag = new MenuMetaData(name, uCode, XmlNodeExtention.Get(level, "Tag"));
                if (XmlNodeExtention.Get(level, "Tag") != null)
                    Maitre.Click += new EventHandler(BuildStrip_Click);

                result.Add(Maitre);

                if(name == "menu")
                {
                    var parcourir = Parcourir(level.ChildNodes);
                    if (parcourir != null)
                        Maitre.DropDownItems.AddRange(parcourir.ToArray());
                }

            }

            return result;
        }

        /// <summary>
        ///  Capture Menu click
        /// <para>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        /// </para>
        /// </summary>
        protected override internal void BuildStrip_Click(object sender, EventArgs e)
          {
              OnMenuClick((ToolStripMenuItem)sender, (MenuMetaData)((ToolStripMenuItem)sender).Tag);
          }

        /// <summary>
        /// Menu clicked
        /// <para>
        /// <param name="toolStripMenuItem">ToolStripMenuItem</param>
        /// <param name="menuMetaData">MenuMetaData</param>
        /// </para>
        /// </summary>
        public abstract void OnMenuClick(ToolStripMenuItem toolStripMenuItem, MenuMetaData menuMetaData);

    }
#else

    /// <summary>
    /// Store data form the xml file
    /// <example> key = ParseKeys("Control+B"); </example>/// 
    /// </summary>
    public class MenuMetaData
    {

        internal MenuMetaData(MenuItem item, string name, ushort code, string tag)
        {
            this.Item = item;
            this.Name = name;
            this.Code = code;
            this.Tag = tag;
        }

         /// <summary>
        /// Name
        /// <returns>string</returns>
        /// </summary>
        public MenuItem Item { get; internal set; }
        

        /// <summary>
        /// Name
        /// <returns>string</returns>
        /// </summary>
        public string Name { get; internal set; }
        /// <summary>
        /// Code
        /// <returns>ushort</returns>
        /// </summary>
        public ushort Code { get; internal set; }
        /// <summary>
        /// Tag
        /// <returns>string</returns>
        /// </summary>
        public string Tag { get; internal set; }
    }


    /// <summary>
    /// Class de contruction of Menu
    /// </summary>
    public abstract class BuildMainStrip : BuildStrip
    {

#if !OldMenu
        List<MenuMetaData> Action = new List<MenuMetaData>(); 
#endif

        /// <summary>
        /// Parse key string
        /// <para>
        /// <param name="filename">string</param>
        /// </para>
        /// <example> new BuildMainStrip("Menu.xml"); </example>/// 
        /// <returns>Keys</returns>
        /// </summary>
        public BuildMainStrip(string filename):
            base (filename)
        {
        }

        /// <summary>
        /// Build data
        /// <para>
        /// <param name="nodelist">XmlNodeList</param>
        /// </para>
        /// <returns>ToolStripItems</returns>
        /// </summary>
        public override ToolStripItems Parcourir(XmlNodeList nodelist)
        {
            MenuItem Maitre = null;
            MenuItem Child = null;
           
            string sFile = null;
            ToolStripItems result = new ToolStripItems();

            foreach (XmlNode level in nodelist)
            {
                string name;
                switch(name = level.Name.ToLower())
                {
                    case "menu":
                        if ((sFile = XmlNodeExtention.Get(level,"File")) != null)
                           {
                                var load = Load(sFile);
                                if (load != null)
                                    result.AddRange(load.ToArray());
                                continue;
                            }
                        break;
                    case "split":
                            Child = new MenuItem();
                            Child.Text = "-";
                            result.Add(Child);
                            continue;
                       // break;
                }

                string sName = XmlNodeExtention.Get(level, "Name");
                ushort uCode = XmlNodeExtention.Get<ushort>(level, "Code");
                string sImage = XmlNodeExtention.Get(level,"Image");
                Maitre = new MenuItem();
                Maitre.Text = sName;
                //Maitre.i, resources != null ? ((System.Drawing.Image)(resources.GetObject(sImage))) : default(Image));

               MenuMetaData mmd = new MenuMetaData(Maitre, name, uCode, XmlNodeExtention.Get(level, "Tag"));
#if OldMenu
                Shortcut Valuekey;

                if ((Valuekey = ParseKeys(XmlNodeExtention.Get(level, "Key"))) != Shortcut.None)
                {
                    Maitre.ShowShortcut = XmlNodeExtention.Get<bool>(level, "Show");
                    Maitre.Shortcut = Valuekey;
                }
                Maitre.Tag = mmd;
#else
                Action.Add(mmd);
#endif

              
                if (XmlNodeExtention.Get(level, "Tag") != null)
                    Maitre.Click += new EventHandler(BuildStrip_Click);

                result.Add(Maitre);

                if(name == "menu")
                {
                    var parcourir = Parcourir(level.ChildNodes);
                    if (parcourir != null)
                    {
#if OldMenu
                        Maitre.MenuItems.AddRange(parcourir.ToArray());
#else
                        foreach (var m in parcourir)
                        {
                            Maitre.MenuItems.Add(m);
                        }
#endif
                    }
                }

            }

            return result;
        }

        /// <summary>
        ///  Capture Menu click
        /// <para>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        /// </para>
        /// </summary>
        protected override internal void BuildStrip_Click(object sender, EventArgs e)
          {

#if OldMenu
                OnMenuClick((MenuItem)sender, (MenuMetaData)((MenuItem)sender).Tag);
#else
              MenuMetaData mmd;
              if ((mmd = Action.Find(p => p.Equals(sender))) != null)
                  OnMenuClick((MenuItem)sender, mmd);
#endif
               
//                
          }

        /// <summary>
        /// Menu clicked
        /// <para>
        /// <param name="toolStripMenuItem">ToolStripMenuItem</param>
        /// <param name="menuMetaData">MenuMetaData</param>
        /// </para>
        /// </summary>
        public abstract void OnMenuClick(MenuItem toolStripMenuItem, MenuMetaData menuMetaData);

    }

#endif
}
